﻿using System;
using Telerik.Web.UI;

public partial class Demos_Menu_TreeView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindTreeView();
        }
    }


    private void BindTreeView()
    {
        new RadTreeViewBinder(true).LoadAllNodes(RadTreeView1);
        RadTreeView1.ExpandAllNodes();
    }

    MenuInfo menuInfo = new MenuInfo();

    #region RadTreeView节点拖放
    /// <summary>
    /// 节点拖放
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void RadTreeView1_NodeDrop(object sender, RadTreeNodeDragDropEventArgs e)
    {
        foreach (RadTreeNode node in e.DraggedNodes)
        {
            PerformDragAndDrop(e.DropPosition, node, e.DestDragNode);
        }
        if (e.DropPosition != RadTreeViewDropPosition.Over)
        {//对当前这一级重新排序
            RadTreeNodeCollection unSortedNodes = e.DestDragNode.Owner.Nodes;
            for (int i = 0; i < unSortedNodes.Count; i++)
            {
                menuInfo.Id = int.Parse(unSortedNodes[i].Value);
                menuInfo.SortIndex = i;
                menuInfo.ChangeSortIndex();
            }
        }
        BindTreeView();
    }

    private void PerformDragAndDrop(RadTreeViewDropPosition dropPosition, RadTreeNode sourceNode,
  RadTreeNode destNode)
    {
        if (sourceNode.Equals(destNode) || sourceNode.IsAncestorOf(destNode))
        {
            return;
        }
        sourceNode.Owner.Nodes.Remove(sourceNode);
        menuInfo.Id = int.Parse(sourceNode.Value);
        if (dropPosition == RadTreeViewDropPosition.Over)
        {
            menuInfo.ParentId = int.Parse(destNode.Value);
            menuInfo.ChangeParent();
        }
        else
        {
            if (dropPosition == RadTreeViewDropPosition.Above)
            {
                // 目标节点之前                
                destNode.InsertBefore(sourceNode);
            }
            else if (dropPosition == RadTreeViewDropPosition.Below)
            {
                // 目标节点之后
                destNode.InsertAfter(sourceNode);
            }
            if (sourceNode.Attributes["parentId"] != destNode.Attributes["parentId"])
            {//改变父节点
                menuInfo.ParentId = int.Parse(destNode.Attributes["parentId"]);
                menuInfo.ChangeParent();
            }
        }
    }
    #endregion

    #region 节点添加/修改/删除
    protected void RadTreeView1_ContextMenuItemClick(object sender, RadTreeViewContextMenuEventArgs e)
    {
        menuInfo.Id = int.Parse(e.Node.Value);
        if (e.MenuItem.Value == "Delete")
        {
            menuInfo.Delete();
            BindTreeView();
            return;
        }

        RadDock1.Visible = true;
        RadDock1.Title = string.Format("{0}  {1}", e.MenuItem.Text, e.Node.Text);

        if (e.MenuItem.Value == "Add")
        {
            menuParentId.Value = e.Node.Value;
            return;
        }
        SetNodeInfoToEditForm(e.Node);
    }

    /// <summary>
    /// 当前编辑的节点信息显示到表单
    /// </summary>
    /// <param name="node"></param>
    void SetNodeInfoToEditForm(RadTreeNode node)
    {
        menuParentId.Value = node.Attributes["parentId"];
        menuId.Value = node.Value;
        menuNameBox.Text = node.Text;
        menuUrlBox.Text = node.Attributes["url"];
        menuIconBox.SelectedValue = node.Attributes["icon"];
    }

    /// <summary>
    /// 取得表单中的节点信息
    /// </summary>
    void GetNodeInfoFromForm()
    {
        menuInfo.Id = int.Parse(menuId.Value);
        menuInfo.ParentId = int.Parse(menuParentId.Value);
        menuInfo.DisplayName = menuNameBox.Text;
        menuInfo.NavigateUrl = menuUrlBox.Text;
        menuInfo.Icon = menuIconBox.SelectedValue;
    }

    protected void RadSave_Click(object sender, EventArgs e)
    {
        GetNodeInfoFromForm();
        if (menuInfo.Id == 0)
        {
            menuInfo.Add();
        }
        else
        {
            menuInfo.Update();
        }
        SetNodeInfoToEditForm(new RadTreeNode(string.Empty, "0"));
        RadDock1.Visible = false;
        BindTreeView();
    }

    protected void RadCancel_Click(object sender, EventArgs e)
    {
        RadDock1.Visible = false;
    }
    #endregion
}